/**
 * @author LKQ
 * @date 2022/2/18 11:40
 * @description 思想：枚举所有子串，然后判断这些子串是否回文 枚举时间复杂度O(n^2) 判断是否回文O(n)，总时间复杂度
 * O(n^3)
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution= new Solution();
        solution.countSubstrings("aaa");
    }
    public int countSubstrings(String s) {
        int count = 0;
        for (int subLength = 1; subLength <= s.length(); subLength++) {
            int start = 0;
            while (start <= s.length() - subLength) {
                if (isHui(s.substring(start, start + subLength))) {
                    count++;
                }
                start++;
            }
        }
        return count;
    }

    public boolean isHui(String s) {
        int n = s.length();
        if (n == 1) {
            return true;
        }
        for (int i = 0; i < n / 2; i++) {
            if (s.charAt(i) != s.charAt(n - i - 1)) {
                return false;
            }
        }
        return true;
    }
}
